package com.example.cinema.drawview;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.LinearInterpolator;

import androidx.annotation.Nullable;

import com.example.cinema.R;

public class RollText  extends View {
    public RollText(Context context) {
        this(context,null);
    }

    public RollText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }
    private int textColors;
    private String textString;
    private float textfontsize;
    private int texttime;
    public RollText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.RollText,defStyleAttr,0);
        textColors = typedArray.getColor(R.styleable.RollText_textscolor, Color.BLACK);
        textString = typedArray.getString(R.styleable.RollText_texts);
        textfontsize= typedArray.getDimension(R.styleable.RollText_textsfont,50);
        texttime = typedArray.getInt(R.styleable.RollText_texttime,5000);
        init();
    }

    private int widths;
    private int height;
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int w = widthMeasureSpec;
        int h = heightMeasureSpec;
        widths = resolveSize(w,widthMeasureSpec);
        height = resolveSize(h,heightMeasureSpec);
        setMeasuredDimension(widths,height);
    }

    private Paint textpaint;

    private void init()
    {
        textpaint = new Paint();
        textpaint.setColor(textColors);
        textpaint.setTextSize(textfontsize);
        textpaint.setAntiAlias(true);
        textpaint.setDither(true);
        textpaint.setStyle(Paint.Style.FILL);

    }
    private float y;
    private ValueAnimator va;
    private float textwidth;
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        y = (height-textfontsize)/2;
        y = y+textfontsize;
        canvas.drawText(textString,x,y,textpaint);
    }
    private float x = 0;
    public void start()
    {
        if(textString!=null) {
            textwidth = textfontsize * (textString.length());
        }else {
            return;
        }
        ValueAnimator va = ValueAnimator.ofFloat(textwidth);
        va.setDuration(texttime);
        va.setInterpolator(new LinearInterpolator());
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float xx  = (float) animation.getAnimatedValue();
                invalidate();
                postInvalidate();
                x = widths -xx;
                if(xx==textwidth)
                {
                    x = widths;
                    start();
                }
            }
        });
        va.start();
    }

    //设置文字
    public void  setTexts(String texts)
    {
        textString = texts;
    }
}
